package br.jcrsistemas.syscomercio.janelas;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.EdgedBalloonStyle;
import net.java.balloontip.utils.TimingUtils;
import br.jcrsistemas.componentes.SysFrame;
import br.jcrsistemas.componentes.SysListComandas;
import br.jcrsistemas.componentes.SysTextField;
import br.jcrsistemas.syscomercio.bean.Comanda;
import br.jcrsistemas.syscomercio.dao.ComandaDAO;
import br.jcrsistemas.util.Configuracoes;
import br.jcrsistemas.util.Util;

public class FrmComandas extends SysFrame {

	private JPanel contentPane;
	private final JLabel lblComandas = new JLabel("Comandas");
	private final JSeparator separator = new JSeparator();
	private SysListComandas sysListComandas = new SysListComandas();
	private final JLabel lblComanda = new JLabel("Comanda: ");
	private final SysTextField txtComanda = new SysTextField();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
					FrmComandas frame = new FrmComandas();
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrmComandas() {
		initialize();
		try {
			UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void gerenciarComanda(Comanda comanda) {
		ComandaDAO dao = new ComandaDAO();
		dao.refreshComanda(comanda);
		if (!comanda.isAberta()) {
			if (JOptionPane.showConfirmDialog(null,
					"Comanda fechada, gostaria de abrir?") == JOptionPane.YES_OPTION) {
				comanda.setAberta(true);
				FrmGerenciaComanda frmComanda = new FrmGerenciaComanda(comanda, true, this);
				frmComanda.setLocationRelativeTo(null);
				frmComanda.setVisible(true);
			}
		} 
		else {
			FrmGerenciaComanda frmComanda = new FrmGerenciaComanda(comanda, true, this);
			frmComanda.setLocationRelativeTo(null);
			frmComanda.setVisible(true);
		}
		refreshComandas();
	
	}
	private void initialize() {
		// hide>>$
		sysListComandas = new SysListComandas(Configuracoes.getTotalComandas());
		// sysListComandas.addListSelectionLisetener(new ListSelectionListener()
		// {
		//
		// @Override
		// public void valueChanged(ListSelectionEvent e) {
		// JList lista = (JList) e.getSource();
		// if(lista.getSelectedValue() instanceof Comanda) {
		// Comanda comanda = (Comanda) lista.getSelectedValue();
		// if(!comanda.isAberta()) {
		// if(JOptionPane.showConfirmDialog(null,
		// "Comanda fechada, gostaria de abrir?") == JOptionPane.YES_OPTION) {
		// comanda.setAberta(true);
		// }
		// }
		// else {
		// if(JOptionPane.showConfirmDialog(null,
		// "Comanda aberta, gostaria de fechar?") == JOptionPane.YES_OPTION) {
		// comanda.setAberta(false);
		// }
		// }
		// // JOptionPane.showMessageDialog(null, "Comanda " +
		// comanda.getNumero());
		// }
		// }
		// });
		sysListComandas.getLista().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() >= 2) {
					JList lista = (JList) e.getSource();
					if (lista.getSelectedValue() instanceof Comanda) {
						Comanda comanda = (Comanda) lista.getSelectedValue();
						gerenciarComanda(comanda);
//						refreshComandas();
					}

				}
				super.mouseClicked(e);
			}
		});
		// hide<<$
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1010, 720);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		lblComandas.setIcon(new ImageIcon(FrmComandas.class
				.getResource("/br/jcrsistemas/icones/table-64.png")));
		lblComandas.setBounds(2, 5, 571, 71);
		lblComandas.setFont(new Font("Segoe Print", Font.BOLD, 20));

		contentPane.add(lblComandas);
		separator.setBounds(-7, 90, 1007, 3);

		contentPane.add(separator);
		sysListComandas.setBounds(2, 132, 979, 433);

		contentPane.add(sysListComandas);
		lblComanda.setBounds(12, 105, 79, 14);

		contentPane.add(lblComanda);
		txtComanda.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				SysTextField componente = (SysTextField) e.getSource();
				componente.setSelectionStart(0);
				componente.setSelectionEnd(componente.getText().length());
			}
		});
		txtComanda.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					Comanda comanda = sysListComandas.getComanda(txtComanda.getText().trim());
					if(comanda == null ) {
						EdgedBalloonStyle style = new EdgedBalloonStyle(Color.WHITE, Color.BLUE);
						BalloonTip balloon = new BalloonTip(txtComanda, new JLabel("Comanda inválida"),style,BalloonTip.Orientation.LEFT_ABOVE, BalloonTip.AttachLocation.ALIGNED, 30, 10, false );
						TimingUtils.showTimedBalloon(balloon, 3000);
//						try {
//							Thread.sleep(6000);
//						} catch (InterruptedException e1) {
//							e1.printStackTrace();
//						}
//						balloon.setVisible(false);
						txtComanda.requestFocus();
						return;
						
					}
					sysListComandas.getLista()
							.setSelectedValue(comanda, true);
				}
			}
		});
		txtComanda.setTamanho(3);
		txtComanda.setApenasNumero(true);
		txtComanda.setBounds(109, 100, 62, 20);
		lblComanda.setLabelFor(txtComanda);
		lblComanda.setDisplayedMnemonic('C');
		contentPane.add(txtComanda);
		
		refreshComandas();
	}
	
	public void refreshComandas() {
		sysListComandas.limpa();
		ComandaDAO dao = new ComandaDAO();
		for(Comanda c: dao.listarTodos()) {
			c.setAberta(true);
			sysListComandas.setComanda(c.getNumero(), c);
		}
		sysListComandas.updateUI();
	}
}
